Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds DeprecatedHelperVisitor #659

Closed
wants to merge 5 commits into from

Conversation

bbrala
Copy link
Contributor

@bbrala bbrala commented Nov 23, 2023

Adds DeprecatedHelperVisitor which removes PHPStan parsing of everything in the 3rd argument of the DeprecationHelper, making it ignore deprecation messages in there.

…ing in the 3rd argument of the DeprecationHelper, making it ignore deprecation messages in there.
@bbrala bbrala force-pushed the feature/deprecation-helper-fixer branch from ba47283 to 47de7e3 Compare November 23, 2023 21:48
@@ -0,0 +1,26 @@
<?php

namespace GroupLegacy;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a more sane namespace i guess

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good enough for the test data :)

{

if ($node instanceof Node\Expr\StaticCall) {
if ($node->class instanceof Node\Name\FullyQualified && $node->class->isFullyQualified() && $node->class->toString() === 'Drupal\Component\Utility\DeprecationHelper') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's always an instance of FullyQualified even if imported, right? Pretty sure but double checking.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i removed this one after your review

Comment on lines +25 to +30
$this->deprecatedCall = null;
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}

return null;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a chance our property won't get unset for some reason?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling deprecation helper without arguments? But that is impossible.

Perhaps if while moving into the function something changes the node, so the argument is no longer on the same line and therefor not equal? Not sure how, and if that is possible.

But the effect would basically be if would check the array a few times extra, which should be quite cheap.

There might be a way to add some more checks when in the argument. Or do something like keeping a stack of the last static call and check if that call is nothing weird.

        if ($node instanceof Node\Expr\StaticCall) {
            $this->staticCall = $node; // this and check this?
            if ($node->class instanceof Node\Name\FullyQualified && $node->class->toString() === 'Drupal\Component\Utility\DeprecationHelper') {
                $this->deprecatedCall = $node->getArgs()[2];
                return null;
            }
        }

But that seems like overkill.

}

if ($this->deprecatedCall !== null && $node instanceof Node\Arg && $this->isSavedArgument($node)) {
$this->deprecatedCall = null;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The null check is technically duplicated in the isSavedArgument method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was making phpstan happy, the property can be null.

private function isSavedArgument(Node\Arg $node): bool
{
if ($this->deprecatedCall !== null && $node->getAttributes() === $this->deprecatedCall->getAttributes()) {
return true;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay so when we detect a static call on the methods, we fetch and stash the callable for the deprecated case. Then when then node parser reaches the argument we check and unset.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes!

if ($this->deprecatedCall !== null && $node instanceof Node\Arg && $this->isSavedArgument($node)) {
$this->deprecatedCall = null;
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the magic, yeah? PHPStan doesn't analyze anything in the tree? Seems problematic and instead we should try adding information if possible. Probably not, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it stops parsing as discussed on slack.

@bbrala
Copy link
Contributor Author

bbrala commented Jan 6, 2024

Hi @mglaman , any chance you can revisit this? I understand the drawbacks, but we need some solution to skip code inside the deprecations helper before we start posting patches.

@mglaman
Copy link
Owner

mglaman commented Jan 6, 2024

:/ sorry it's been in my TODOs

Screenshot_20240106-085455.png

I'll get to this shortly. I'm thinking we can enable it behind a feature flag. The project update bot can opt in. I'm still concerned that it just halts analysis.

@bbrala
Copy link
Contributor Author

bbrala commented Jan 11, 2024

This has been superceeded by #714, yay for phpstan developer @ondrejmirtes (and you @mglaman ;))

@bbrala bbrala closed this Jan 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants